using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace Ent.Web.Helper
{
    /// <summary>
    /// Class XMLHelper contains all methods and functions that deals with XML files
    /// </summary>
    public class XMLHelper
    {
        private XmlDocument xmlDoc;
        private string XmlFilePath;
        /// <summary>
        /// Initialises XMLHelper object.
        /// </summary>
        /// <param name="path">Path of the Xml file</param>
        public XMLHelper(string path)
        {
            try
            {
                XmlFilePath = path;
                xmlDoc = new XmlDocument();
                if (System.IO.File.Exists(path))
                    LoadXML(path);
                else
                    throw new Exception("File " + path + " do not exists.", new Exception());
            }
            catch (Exception ex)
            {
                throw new Exception("Error initialising XMLHelper object. " + ex.Message, new Exception("Error at XMLHelper.XMLHelper : " + ex.Message));
            }
        }
        /// <summary>
        /// Loads Xml file to XMLDocument object.
        /// </summary>
        /// <param name="path">Path of the xml file</param>
        private void LoadXML(string path)
        {
            try
            {
                XmlTextReader xmlReader = new XmlTextReader(path);
                xmlReader.WhitespaceHandling = WhitespaceHandling.None;
                xmlDoc.Load(xmlReader);
                xmlReader.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading XML file '" + path + "'.", new Exception("Error at XMLHelper.LoadXML : " + ex.Message));
            }
        }
        /// <summary>
        /// Returns attribute value for first attribute with given name.
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <param name="attribute">Attribute to search</param>
        /// <returns>Returns value of attribute</returns>
        public string GetAttributeForSingleNode(string xpath, string attribute)
        {
            string retValue = "";
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                XmlAttribute xAttr = xNode.Attributes[attribute];
                if (xAttr != null)
                    retValue = xAttr.Value;
                else retValue = "";
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching attribute value for attribute '" + attribute + "'.", new Exception("Error at XMLHelper.GetAttributeForSingleNode : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns array of values of all attributes with given name
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <param name="attribute">Attribute to search</param>
        /// <returns>Returns value array of attribute</returns>
        public string[] GetAttributesOfAllNodes(string xpath, string attribute)
        {
            string[] retValue = null;
            try
            {
                XmlNodeList xNodeList = xmlDoc.SelectNodes(xpath);
                retValue = new string[xNodeList.Count];
                int index = 0;
                foreach (XmlNode xNode in xNodeList)
                {
                    retValue.SetValue(xNode.Attributes[attribute].Value, index++);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching attribute value for attribute '" + attribute + "'.", new Exception("Error at XMLHelper.GetAttributesOfAllNodes : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns the array of all the child nodes at given path
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns node name array of all child nodes</returns>
        public string[] GetChildNodes(string xpath)
        {
            string[] retValue = null;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                XmlNodeList xNodeList = xNode.ChildNodes;
                retValue = new string[xNodeList.Count];
                int index = 0;
                foreach (XmlNode node in xNodeList)
                {
                    retValue.SetValue(node.Name, index++);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching Child nodes.", new Exception("Error at XMLHelper.GetChildNodes : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns id of all the child nodes at given path
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns array of ids of all child nodes</returns>
        public string[] GetChildNodesId(string xpath)
        {
            string[] retValue = null;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    return null;
                XmlNodeList xNodeList = xNode.ChildNodes;
                retValue = new string[xNodeList.Count];
                int index = 0;
                foreach (XmlNode node in xNodeList)
                {
                    string id = node.Attributes["id"].Value;
                    retValue.SetValue(id, index++);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching id of Child nodes.", new Exception("Error at XMLHelper.GetChildNodesId : " + ex.Message));
            }
            return retValue;
        }
        public DataTable GetChildNodesAsDataTable(string xpath, string nodeName)
        {
            DataTable retValue = new DataTable(nodeName);
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    return null;
                XmlNodeList xNodeList = xNode.SelectNodes(nodeName);
                System.Collections.ArrayList colArray = new System.Collections.ArrayList();
                foreach (XmlNode node in xNodeList)
                {
                    foreach (XmlAttribute xAttr in node.Attributes)
                    {
                        if (!colArray.Contains(xAttr.Name))
                        {
                            retValue.Columns.Add(xAttr.Name);
                            colArray.Add(xAttr.Name);
                        }
                    }
                }
                foreach (XmlNode node in xNodeList)
                {
                    DataRow dr = retValue.NewRow();
                    foreach (XmlAttribute xAttr in node.Attributes)
                    {
                        dr[xAttr.Name] = xAttr.Value;
                    }
                    retValue.Rows.Add(dr);
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching id of Child nodes.", new Exception("Error at XMLHelper.GetChildNodesId : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns a string array of all the child nodes
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <param name="attributeName">Attribute to search</param>
        /// <returns>Returns array of values for given attribute</returns>
        public string[] GetChildNodesAttributeValue(string xpath, string attributeName)
        {
            string[] retValue = null;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    return null;
                XmlNodeList xNodeList = xNode.ChildNodes;
                retValue = new string[xNodeList.Count];
                int index = 0;
                foreach (XmlNode node in xNodeList)
                {
                    string id = node.Attributes[attributeName].Value;
                    retValue.SetValue(id, index++);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching " + attributeName + " of Child nodes.", new Exception("Error at XMLHelper.GetChildNodesAttributeValue : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns inner Text element for the given node 
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns inner text value</returns>
        public string GetInnerText(string xpath)
        {
            string retValue = "";
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                else
                    retValue = xNode.InnerText;
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching innerText." + ex.Message, new Exception("Error at XMLHelper.GetInnerText : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns inner XML element for the given node 
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns inner xml text</returns>
        public string GetInnerXml(string xpath)
        {
            string retValue = "";
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                else
                    retValue = xNode.InnerXml;
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching inner Xml.", new Exception("Error at XMLHelper.GetInnerXml : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns the outerxml as a string
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns outer xml</returns>
        public string GetOuterXml(string xpath)
        {
            string retValue = "";
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                else
                    retValue = xNode.OuterXml;
            }
            catch (Exception ex)
            {
                throw new Exception("Error fetching inner Xml.", new Exception("Error at XMLHelper.GetOuterXml : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Checks if the given node contains any child nodes or not.
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns true if given node contains child nodes</returns>
        public bool HasChildNode(string xpath)
        {
            bool retValue = false;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                retValue = xNode.HasChildNodes;
            }
            catch (Exception ex)
            {
                throw new Exception("Error checking for child nodes.", new Exception("Error at XMLHelper.HasChildNode : " + ex.Message));
            }
            return retValue;
        }
        public bool HasNonTextChildNode(string xpath)
        {
            bool retValue = false;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    throw new Exception("Node not be found at '" + xpath + "' in " + xmlDoc.BaseURI, new Exception());
                if (xNode.HasChildNodes)
                {
                    if (!(xNode.ChildNodes.Count == 1 && (xNode.ChildNodes[0].NodeType == XmlNodeType.Text || xNode.ChildNodes[0].NodeType == XmlNodeType.CDATA)))
                        retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error checking for child nodes.", new Exception("Error at XMLHelper.HasChildNode : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns true if the node exists
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <returns>Returns true if node exists</returns>
        public bool IfNodeExist(string xpath)
        {
            bool retValue = false;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode != null)
                    retValue = true;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.HasNode : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns a string value of an array of XMLNode
        /// </summary>
        /// <param name="nodeArray">Array on XmlNode</param>
        /// <returns>Serialises XmlNode array to plain text</returns>
        public string ConvertToString(XmlNode[] nodeArray)
        {
            try
            {
                string retValue = "";
                foreach (XmlNode xn in nodeArray)
                {
                    retValue += xn.OuterXml;
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checkingconverting to string.", new Exception("Error at XMLHelper.ConvertToString : " + ex.Message));
            }
        }
        /// <summary>
        /// Returns true if a node passed has been added
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <param name="innerXml">Inner Xml value</param>
        /// <returns>Returns true if the operation is performed successfully</returns>
        public bool AddNode(string xpath, string innerXml)
        {
            bool retValue = false;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode != null)
                {
                    xNode.InnerXml += innerXml;
                    xmlDoc.Save(XmlFilePath);
                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.HasNode : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Updates a node by adding the inner xml content
        /// </summary>
        /// <param name="xpath">XPath of parent node</param>
        /// <param name="innerXml">Inner Xml Content</param>
        /// <returns>Returns true when operation is performed successfully</returns>
        public bool UpdateNode(string xpath, string innerXml)
        {
            bool retValue = false;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode != null)
                {
                    xNode.InnerXml = innerXml;
                    xmlDoc.Save(XmlFilePath);
                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.UpdateNode : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns the count of elements with the same node name
        /// </summary>
        /// <param name="nodeName">Node Name</param>
        /// <returns>Returns the count of elements with the same node name</returns>
        public int dGetNodeCount(string nodeName)
        {
            try
            {
                XmlNodeList xNodeList = xmlDoc.GetElementsByTagName(nodeName);
                return xNodeList.Count;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.dGetNodeCount : " + ex.Message));
            }
        }
        /// <summary>
        /// Returns the attribute value for the node at the specified index
        /// </summary>
        /// <param name="nodeName">Node Name</param>
        /// <param name="attributeName">Name of Attribute</param>
        /// <param name="index">Index of Node</param>
        /// <returns>Returns the attribute value for the node at the specified index</returns>
        public string dGetAttributeValueForNodeAtIndex(string nodeName, string attributeName, int index)
        {
            try
            {
                XmlNodeList xNodeList = xmlDoc.GetElementsByTagName(nodeName);
                if (xNodeList.Count > index)
                {
                    XmlNode xNode = xNodeList.Item(index);
                    if (IfNodeHasAttribute(xNode, attributeName))
                        return ((XmlAttribute)xNode.Attributes.GetNamedItem(attributeName)).Value;
                    else
                        throw new Exception("Attribute not found.");
                }
                else throw new Exception("Index is out of the bound.");
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.dGetAttributeValueForNodeAtIndex : " + ex.Message));
            }
        }
        /// <summary>
        /// Returns true if the dpecifies node has the passed attribute
        /// </summary>
        /// <param name="xNode">XmlNode object</param>
        /// <param name="attribute">Attribute name</param>
        /// <returns>Returns true if the dpecifies node has the passed attribute</returns>
        public bool IfNodeHasAttribute(XmlNode xNode, string attribute)
        {
            try
            {
                bool retValue = false;
                foreach (XmlAttribute xAttr in xNode.Attributes)
                {
                    if (xAttr.Name == attribute)
                    {
                        retValue = true;
                        break;
                    }
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.IfNodeHasAttribute : " + ex.Message));
            }
        }

        /// <summary>
        /// This method returns a XML string for the GUI
        /// </summary>
        /// <param name="xNode">XmlNode Object</param>
        /// <param name="iteration">Iteration required for indentation</param>
        /// <returns>Returns serialised text</returns>
        public static string CreateStringForGUI(XmlNode xNode, int iteration)
        {
            try
            {
                string retValue = "";
                retValue += "<br/>";
                for (int cnt = 0; cnt < iteration; cnt++)
                {
                    retValue += "&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                retValue += "&lt;" + xNode.Name;
                for (int i = 0; i < xNode.Attributes.Count; i++)
                {
                    XmlAttribute xAtt = xNode.Attributes[i];
                    retValue += " " + xAtt.Name + "=\"" + xAtt.Value + "\"";
                }
                retValue += "&gt;";
                for (int i = 0; i < xNode.ChildNodes.Count; i++)
                {
                    retValue += CreateStringForGUI(xNode.ChildNodes[i], iteration + 1);
                }

                retValue += "<br/>";
                for (int cnt = 0; cnt < iteration; cnt++)
                {
                    retValue += "&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                retValue += "&lt;/" + xNode.Name + "&gt;";
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.CreateStringForGUI : " + ex.Message));
            }
        }


        /// <summary>
        /// Converts XmlNodeList to XmlNode[]
        /// </summary>
        /// <param name="nodeList">object of XmlNodeList</param>
        /// <returns>Returns Array of XmlNode</returns>
        public static XmlNode[] ConvertToArray(XmlNodeList nodeList)
        {
            XmlNode[] retValue = new XmlNode[nodeList.Count];
            try
            {
                for (int i = 0; i < nodeList.Count; i++)
                {
                    retValue.SetValue(nodeList[i], i);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.ConvertToArray : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Returns the XML attribute 
        /// </summary>
        /// <param name="xNode">XmlNode object</param>
        /// <param name="attributeCaseNames">Attribute name</param>
        /// <returns>Returns first found XmlAttribute with name present in attributeCaseNames</returns>
        public static XmlAttribute GetAttribute(XmlNode xNode, string[] attributeCaseNames)
        {
            try
            {
                XmlAttribute retValue = null;
                foreach (string s in attributeCaseNames)
                {
                    retValue = (XmlAttribute)xNode.Attributes.GetNamedItem(s);
                    if (retValue != null)
                        break;
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.GetAttribute : " + ex.Message));
            }
        }
        public Dictionary<string,string> GetNameValuePair(string xPath, string nodeName, string keyAttributeName, string valueAttributeName)
        {
            try
            {
                Dictionary<string, string> retValue = new Dictionary<string, string>();
                XmlNodeList xnl = xmlDoc.SelectNodes(xPath + "/" + nodeName);
                foreach (XmlNode xn in xnl)
                {
                    if (xn.Attributes[keyAttributeName] != null)
                    {
                        string key = xn.Attributes[keyAttributeName].Value;
                        string value = "";
                        if (valueAttributeName.ToLower() == "<innertext>")
                        {
                            if (xn.InnerText != "")
                                value = xn.InnerText;
                        }
                        else
                        {
                            if (xn.Attributes[valueAttributeName] != null)
                                value = xn.Attributes[valueAttributeName].Value;
                        }
                        retValue.Add(key, value);
                    }
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.GetKeyValuePair : " + ex.Message));
            }
        }
        public KeyValueHandler GetKeyValuePair(string xPath, string nodeName, string keyAttributeName, string valueAttributeName)
        {
            try
            {
                string kvString = "";
                XmlNodeList xnl = xmlDoc.SelectNodes(xPath + "/" + nodeName);
                foreach (XmlNode xn in xnl)
                {
                    if (xn.Attributes[keyAttributeName] != null)
                    {
                        string key = xn.Attributes[keyAttributeName].Value;
                        string value = "";
                        if (valueAttributeName.ToLower() == "<innertext>")
                        {
                            if (xn.InnerText != "")
                                value = xn.InnerText;
                        }
                        else
                        {
                            if (xn.Attributes[valueAttributeName] != null)
                                value = xn.Attributes[valueAttributeName].Value;
                        }
                        kvString += key + "=" + value + ";";
                    }
                }
                string[] keys = this.GetChildNodesAttributeValue(xPath, keyAttributeName);
                return new KeyValueHandler(kvString);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.GetKeyValuePair : " + ex.Message));
            }
        }
        public DataTable GetChildNodesAsDataTable(string xpath, string nodeName, string[] attributeList)
        {
            DataTable retValue = new DataTable(nodeName);
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode == null)
                    return null;
                XmlNodeList xNodeList = xNode.SelectNodes(nodeName);
                System.Collections.ArrayList colArray = new System.Collections.ArrayList();
                foreach (string attr in attributeList)
                {
                    if (attr.ToLower() == "<innertext>")
                        retValue.Columns.Add("innertext");
                    else
                        retValue.Columns.Add(attr);
                }
                foreach (XmlNode node in xNodeList)
                {
                    DataRow dr = retValue.NewRow();
                    foreach (string attr in attributeList)
                    {
                        foreach (XmlAttribute xAttr in node.Attributes)
                        {
                            if (xAttr.Name.ToLower() == attr.ToLower())
                                dr[attr] = xAttr.Value;
                        }
                        if (attr.ToLower() == "<innertext>")
                            dr["innertext"] = node.InnerText;
                    }
                    retValue.Rows.Add(dr);
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.GetChildNodesAsDataTable : " + ex.Message));
            }
            return retValue;
        }
        /// <summary>
        /// Encodes the different xml tags for display on GUI
        /// </summary>
        /// <param name="text">Text to encode</param>
        /// <returns>Returns encoded text</returns>
        public static string XmlEncode(string text)
        {
            try
            {
                string retValue = text;
                retValue = retValue.Replace("&", "&amp;");
                retValue = retValue.Replace("<", "&lt;");
                retValue = retValue.Replace(">", "&gt;");
                retValue = retValue.Replace("\"", "&quot;");
                retValue = retValue.Replace("\n", "&#92;n");
                retValue = retValue.Replace("\r", "&#92;r");
                retValue = retValue.Replace("\t", "&#92;t");
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.XmlEncode : " + ex.Message));
            }
        }
        /// <summary>
        /// Decodes the text to xml tags
        /// </summary>
        /// <param name="text">Text to dcode</param>
        /// <returns>Decoded Text</returns>
        public static string XmlDecode(string text)
        {
            try
            {
                string retValue = text;
                retValue = retValue.Replace("&amp;", "&");
                retValue = retValue.Replace("&lt;", "<");
                retValue = retValue.Replace("&gt;", ">");
                retValue = retValue.Replace("&quot;", "\"");
                retValue = retValue.Replace("&#92;r", "\r");
                retValue = retValue.Replace("&#92;n", "\n");
                retValue = retValue.Replace("&#92;t", "\t");
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.XmlDecode : " + ex.Message));
            }
        }
        /// <summary>
        /// Returns the first xml child node
        /// </summary>
        /// <param name="parentNode">Parent Node</param>
        /// <param name="nodeName">Node Name</param>
        /// <returns>Returns the first xml child node</returns>
        public static XmlNode GetFirstChildNode(XmlNode parentNode, string nodeName)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.InnerXml = parentNode.OuterXml;
                return xDoc.SelectSingleNode("//" + nodeName);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.GetFirstChildNode : " + ex.Message));
            }
        }

        /// <summary>
        /// Remove the child nodes of the node passed as the parameter
        /// </summary>
        /// <param name="xpath">Xpath to search</param>
        /// <returns>returns true if the child nodes have been removed</returns>
        public bool ReplaceNodeForTableSchema(string xpath, string innerXml)
        {
            bool retValue = false;
            try
            {
                XmlNode xNode = xmlDoc.SelectSingleNode(xpath);
                if (xNode != null)
                {
                    xNode.InnerXml = innerXml;
                    xmlDoc.Save(XmlFilePath);
                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while checking for node Existance'.", new Exception("Error at XMLHelper.ReplaceNodeForTableSchema : " + ex.Message));
            }
            return retValue;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool SaveXML(string path)
        {
            bool retValue = false;
            try
            {
                xmlDoc.Save(path);
                retValue = true;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while saving xml file'.", new Exception("Error at XMLHelper.SaveXML : " + ex.Message));
            }
            return retValue;
        }
    }
}
